﻿using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LightController.Frm
{
    public partial class Dlg_LightControllerDebug1 : Form
    {
        [NonSerialized]
        private ILightController _lightController;
        public ILightController LightController
        {
            get
            {
                return _lightController;
            }
            set
            {
                _lightController = value;
            }
        }

        [NonSerialized]
        private int _chs = 4;
        [NonSerialized]
        string _name;
        [NonSerialized]
        private string _comAddress;

        public int Chs
        {
            get
            {
                return _chs;
            }
        }
        public string ControllerName
        {
            get
            {
                return _name;
            }
        }

        public string ComAddress
        {
            get
            {
                return _comAddress;
            }
        }
        public Dlg_LightControllerDebug1()
        {
            InitializeComponent();

        }

        private void trackBarLight_Scroll(object sender, EventArgs e)
        {
            int lightVal = trackBarLight.Value;
            try
            {
                LightController?.SetIntensity((int)num_CH.Value, lightVal);
            }
            catch { }
        }

        private void trackBarLight_ValueChanged(object sender, EventArgs e)
        {
            tb_LightValue.Text = trackBarLight.Value.ToString();
        }

        private void tb_LightValue_ValueChanged(object sender, EventArgs e)
        {
            trackBarLight.Value = int.Parse(tb_LightValue.Value.ToString().Trim());
        }

        private void btn_Connect_Click(object sender, EventArgs e)
        {
            if (LightController==null)
            {
                tb_Status.Text = "请选中左侧光源！";
            }
            Task.Run(() =>
            {
                try
                {
                    LightController?.Connect();
                }
                catch { }
            });
        }

        private void btn_Disconnect_Click(object sender, EventArgs e)
        {
            if (LightController == null)
            {
                tb_Status.Text = "请选中左侧光源！";
            }
            try
            {
                LightController?.Disconnect();
            }
            catch { }
        }

        private void btn_OpenAll_Click(object sender, EventArgs e)
        {
            if (LightController == null)
            {
                tb_Status.Text = "请选中左侧光源！";
            }
            try
            {
                LightController?.OpenAllLight();
            }
            catch { }
        }

        private void btn_CloseAll_Click(object sender, EventArgs e)
        {
            if (LightController == null)
            {
                tb_Status.Text = "请选中左侧光源！";
            }
            try
            {
                LightController?.CloseAllLight();
            }
            catch { }
        }

        private void Dlg_LightControllerDebug_Load(object sender, EventArgs e)
        {
            if (LightController == null)
            {
                tb_Status.Text = "请选中左侧光源！";
                return;
            }
            this.Text = LightController.Name + $"[{LightController.CommunicationAddress}]";
            num_LightCHs.Value = LightController.ChanelCount;
            LightController.PropertyChanged += LightController_PropertyChanged;
        }

        private void LightController_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            tb_Status.Text = e.PropertyName;
        }
        private void Dlg_LightControllerDebug_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (LightController == null)
            {
                tb_Status.Text = "请选中左侧光源！";
                return;
            }
           // LightController.PropertyChanged -= LightController_PropertyChanged;
        }

    }
}
